package com.ez.supervise.action.sys;

import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

import com.ez.supervise.action.base.BaseJsonAction;
import com.ez.supervise.core.Page;
import com.ez.supervise.model.sys.RolePermission;
import com.ez.supervise.service.RolePermissionService;

public class RolePermissionAction extends BaseJsonAction {
    private static final long serialVersionUID = 8735170970520475073L;
    private Long[] ids;
    private RolePermissionService rolePermissionService;
    private RolePermission rolePermission;
    private Page page;
    private Long roleId;

    public String saveRolePermission() {
	try {
	    rolePermissionService.saveRolePermissionByRoleId(roleId,ids);
	    outJsonString(RESULT_SUCCESS);
	} catch (Exception e) {
	    outJsonString(RESULT_FAIL);
	}
	return SUCCESS;
    }

    public String findAllRolePermission() {
	page = new Page();
	page.setRoot(rolePermissionService.findPermissionByRoleId(roleId));
	page.setTotalProperty(page.getRoot().size());
	JsonConfig config = new JsonConfig();
	config.setExcludes(new String[] { "creator", "createTime", ",modifier", "modifyTime", "isDeleted", "new", });

	JSONObject jsonObj = JSONObject.fromObject(page, config);
	// System.out.println(jsonObj.toString());
	outJsonString(jsonObj.toString());
	return SUCCESS;
    }

    /**
     * @return
     */
    public String deleteRolePermission() {
	return SUCCESS;
    }

    /**
     * @return
     * @throws Exception
     */
    public String updateRolePermission() throws Exception {
	String fieldName = getRequest().getParameter("fieldName");
	String fieldValue = getRequest().getParameter("fieldValue");
	String strRolePermissionId = getRequest().getParameter("id");
	try {
//	    if (id != null && id > 0) {
//		RolePermission saveRolePermission = new RolePermission();
//		saveRolePermission.setId(id);
//		BeanUtils.setProperty(saveRolePermission, fieldName, fieldValue);
//		rolePermissionService.update(saveRolePermission);
//	    }
	    outJsonString(RESULT_SUCCESS);
	} catch (Exception e) {
	    outJsonString(RESULT_FAIL);
	}

	return SUCCESS;
    }

    public static void main(String[] args) {
	RolePermission rolePermission = new RolePermission();
	rolePermission.setId(1L);
	// rolePermission.setName("linlinyu");
	JSONObject json = JSONObject.fromObject(rolePermission);

	System.out.println(json);

	JsonConfig config = new JsonConfig();
	config.setExcludes(new String[] { "createTime", "modifyTime" });

	JSONObject jsonObj = JSONObject.fromObject(rolePermission, config);
	System.out.println(jsonObj.toString());

    }

    public Long[] getIds() {
	return ids;
    }

    public void setIds(Long[] ids) {
        this.ids = ids;
    }

    public void setRolePermissionService(RolePermissionService rolePermissionService) {
	this.rolePermissionService = rolePermissionService;
    }

    public RolePermissionService getRolePermissionService() {
	return rolePermissionService;
    }

    public void setRolePermission(RolePermission rolePermission) {
	this.rolePermission = rolePermission;
    }

    public RolePermission getRolePermission() {
	return rolePermission;
    }

    public void setRoleId(Long roleId) {
	this.roleId = roleId;
    }

    public Long getRoleId() {
	return roleId;
    }

//    public void setIds(Long ids[]) {
//	this.ids = ids;
//    }
}
